#!/bin/bash
#auto make install LAMP
#by authors kevin 2018/11/9

###########################################apache###################################################
#httpd define path variable
H_URL=http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.37.tar.bz2
H_FILE=`echo $H_URL|awk -F / '{print $NF}'`
H_FILE_DIR=`echo $H_FILE|sed -E 's/\.[tar.bzg2ip]{1,}$//g'`
H_PREFIX=/usr/local/$H_FILE_DIR/
#apr define path variable
APR_URL=http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.5.tar.bz2
APR_FILE=`echo $APR_URL|awk -F / '{print $NF}'`
APR_FILE_DIR=`echo $APR_FILE|sed -E 's/\.[tar.bzg2ip]{1,}$//g'`
APR_PREFIX=/usr/local/$APR_FILE_DIR/
#apr-util define variable
APR_UTIL_URL=http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
APR_UTIL_FILE=`echo $APR_UTIL_URL|awk -F / '{print $NF}'`
APR_UTIL_FILE_DIR=`echo $APR_UTIL_FILE|sed -E 's/\.[tar.bzg2ip]{1,}$//g'`
APR_UTIL_PREFIX=/usr/local/$APR_UTIL_FILE_DIR/
#expat define variable
EXPAT_URL=https://github.com/libexpat/libexpat/releases/download/R_2_2_6/expat-2.2.6.tar.bz2
EXPAT_FILE=`echo $EXPAT_URL|awk -F / '{print $NF}'`
EXPAT_FILE_DIR=`echo $EXPAT_FILE|sed -E 's/\.[tar.bzg2ip]{1,}$//g'`
EXPAT_PREFIX=/usr/local/$EXPAT_FILE_DIR/
#pcre define variable
PCRE_URL=https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.bz2
PCRE_FILE=`echo $PCRE_URL|awk -F / '{print $NF}'`
PCRE_FILE_DIR=`echo $PCRE_FILE|sed -E 's/\.[tar.bzg2ip]{1,}$//g'`
PCRE_PREFIX=/usr/local/$PCRE_FILE_DIR/

##########################mysql############################
CMAKE_URL=
#menu for option [1--apache;2--mysql;3--php;4--config lamp service]
if [ -z "$1" ];then
       echo -e "\033[36mPlease select install menu follow:\033[0m"      
       echo -e "\033[36m1)Make install Apache server\033[0m"      
       echo -e "\033[36m2)Make install MYSQL server\033[0m"      
       echo -e "\033[36m3)Make install PHP\033[0m"      
       echo -e "\033[36m4)Configure index.php and start LAMP service\033[0m"      
       echo -e "\033[36m5)Usage: { /bin/sh $0 1|2|3|4|help}\033[0m"      
       exit
fi
#Install apache
if [ $1 -eq 1 ];then
       echo -e "\033[36mStart install libs...\033[0m"      
	wget -c $EXPAT_URL &&tar -jxvf $EXPAT_FILE &&cd $EXPAT_FILE_DIR ;./configure --without-docbook --prefix=$EXPAT_PREFIX &&make &&make install &&cd ..
	if [ $? -eq 0 ];then
      		 echo -e "\033[36mThe $EXPAT_FILE_DIR install successfully!\033[0m"      
	else
      		 echo -e "\033[36mThe $EXPAT_FILE_DIR install failed!\033[0m"      
		 exit
	fi
	wget -c $PCRE_URL &&tar -jxvf $PCRE_FILE &&cd $PCRE_FILE_DIR ;./configure --prefix=$PCRE_PREFIX &&make &&make install &&cd ..
	if [ $? -eq 0 ];then
      		 echo -e "\033[36mThe $PCRE_FILE_DIR install successfully!\033[0m"      
	else
      		 echo -e "\033[36mThe $PCRE_FILE_DIR install failed!\033[0m"      
		 exit
	fi
	wget -c $APR_URL &&tar -jxvf $APR_FILE &&cd $APR_FILE_DIR ;./configure --prefix=$APR_PREFIX &&make &&make install &&cd ..	
	if [ $? -eq 0 ];then
      		 echo -e "\033[36mThe $APR_FILE_DIR install successfully!\033[0m"      
	else
      		 echo -e "\033[36mThe $APR_FILE_DIR install failed!\033[0m"      
		 exit
	fi
	wget -c $APR_UTIL_URL &&tar -jxvf $APR_UTIL_FILE &&cd $APR_UTIL_FILE_DIR ;./configure --prefix=$APR_UTIL_PREFIX --with-apr=$APR_PREFIX --with-expat=$EXPAT_PREFIX &&make &&make install &&cd ..
	if [ $? -eq 0 ];then
      		 echo -e "\033[36mThe $APR_UTIL_FILE_DIR install successfully!\033[0m"      
	else
      		 echo -e "\033[36mThe $APR_UTIL_FILE_DIR install failed!\033[0m"      
		 exit
	fi
	wget -c $H_URL &&tar -jxvf $H_FILE &&cd $H_FILE_DIR ;./configure \
	--prefix=$H_PREFIX \
	--with-apr-util=$APR_UTIL_PREFIX \
	--with-apr=$APR_PREFIX \
	--with-pcre=$PCRE_PREFIX \
	--enable-so \
	--enable-cache \
	--enable-disk-cache \
	--enable-file-cache \
	--enable-mem-cache \
	--enable-cgi \
	--enable-rewrite \
	--enable-zlib \
	--enable-module=most \
	&&make &&make install &&cd ..
	if [ $? -eq 0 ];then    
  		 if [ ! -f "/usr/bin/httpd" ];then
		 	ln -s /usr/local/$H_FILE_DIR/bin/apachectl /usr/bin/httpd
		 else
		 rm -f /usr/bin/httpd
		 ln -s /usr/local/$H_FILE_DIR/bin/apachectl /usr/bin/httpd
		 fi
		 echo -e "\033[36mThe $H_FILE_DIR install successfully!\033[0m"
  		 echo -e "\033[36mUsage:{httpd [-k start|restart|graceful|graceful-stop|stop]}"
	else
      		 echo -e "\033[36mThe $H_FILE_DIR install failed!\033[0m"      
		 exit
	fi
fi
